package com.beemans.common.app.ui.fragments

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.View
import com.beemans.common.app.R
import com.beemans.common.app.databinding.FragmentLocationBinding
import com.beemans.common.app.ui.base.BaseFragment
import com.beemans.common.ext.setPageBack
import com.beemans.common.ext.startLocation
import com.beemans.common.ext.toString2
import com.beemans.common.utils.PermissionHelper
import com.blankj.utilcode.util.SpanUtils
import com.hjq.permissions.Permission
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.base.dialog.loading.Loader
import com.tiamosu.fly.ext.clickNoRepeat
import java.util.Locale

/**
 * @author tiamosu
 * @date 2021/8/11.
 */
class LocationFragment : BaseFragment() {
    private val dataBinding: FragmentLocationBinding? by lazyDataBindingOrNull { }

    private var title: String? = null

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_location)
    }

    override fun initParameters(bundle: Bundle?) {
        title = getString(MainFragment.PARAM_TITLE)
    }

    override fun initView(rootView: View?) {
        dataBinding?.locationTitleBar?.setTvTitle { text = title }
    }

    override fun initEvent() {
        dataBinding?.locationTitleBar?.setPageBack { }

        dataBinding?.locationBtnGet?.clickNoRepeat {
            getLocation()
        }
    }

    /**
     * 获取位置信息
     */
    @SuppressLint("MissingPermission")
    private fun getLocation() {
        fun start() {
            startLocation(config = {
                locale = Locale.ENGLISH
            }) {
                onLocationStart {
                    Log.e("susu", "onLocationStart")
                    Loader.showLoading(300)
                }
                onLocationFailed {
                    Loader.hideLoading()
                    showToast(it)
                }
                onLocationSuccess { location, address ->
                    Loader.hideLoading()
                    val msg = R.string.common_location_successful.toString2(context)
                    showToast(msg)

                    Log.e(
                        "susu",
                        "mLongitude:${address.longitude}\n" +
                                "mLatitude:${address.latitude}\n" +
                                "mCountryName:${address.countryName}\n" +
                                "mCountryCode:${address.countryCode}\n" +
                                "mPostalCode:${address.postalCode}\n" +
                                "mPremises:${address.premises}\n" +
                                "mAdminArea:${address.adminArea}\n" +
                                "mSubAdminArea:${address.subAdminArea}\n" +
                                "mLocality:${address.locality}\n" +
                                "mSubLocality:${address.subLocality}\n" +
                                "mThoroughfare:${address.thoroughfare}\n" +
                                "mSubThoroughfare:${address.subThoroughfare}\n"
                    )

                    SpanUtils.with(dataBinding?.locationTvContent)
                        .append("location:$location\n\naddress:$address")
                        .create()
                }
            }
        }
        PermissionHelper.with(this)
            .permission(Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION)
            .onForwardToSettings { true }
            .request { grantedList, _, _, _ ->
                if (grantedList.isEmpty()) return@request
                start()
            }
    }

    override fun doBusiness() {
    }
}